<template>
    <div class="LoginPage">
        <el-card class="box-card" shadow="never">
            <el-form ref="form" :model="form" :rules="rules">
                <div class="logo">
                    <img src="../../assets/logo.png" alt="">
                </div>
                <el-form-item prop="account">
                    <el-input v-model="form.account" placeholder="账户"></el-input>
                    <img src="../../assets/login/u91.svg" alt="">
                </el-form-item>
                <el-form-item prop="password">
                    <el-input v-model="form.password" placeholder="密码" show-password></el-input>
                    <img src="../../assets/login/u92.svg" alt="">
                </el-form-item>
                <el-form-item prop="code">
                    <el-input class="codeInp" v-model="form.code" @keydown.native.enter="login" placeholder="验证码"
                              type="number"></el-input>
                    <img src="../../assets/login/u92.svg" alt="">
                    <img class="code" @click="getCode" :src="codeImg" alt="">
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" :loading="loading" @click="login">登录</el-button>
                </el-form-item>

                <p class="tips">
                    仅用于IT培训教学使用，为保障您的个人信息安全，请勿向平台录入任何个人敏感信息（如手机号、身份证号等）！
                </p>
            </el-form>
        </el-card>
        <div class="copyright">
            <span>江苏传智播客教育科技股份有限公司</span>
            <span>版权所有Copyright 2006-2022 All Rights Reserved</span>
            <span>苏ICP备16007882号-11</span>
        </div>
        <video src="../../assets/login/video.mp4" loop autoplay muted></video>
    </div>
</template>

<script>
import { getCode } from '@/api/login'
import { randomKey } from '@/utils/randomKey'

export default {
    name: 'LoginPage',
    created () {
        this.form.key = randomKey(24, 36)
        this.getCode()
    },
    data () {
        return {
            form: {
                account: 'demo',
                password: 'hm#qd@23!',
                code: '',
                tenant: '0000',
                bindAccount: '',
                bindPassword: '',
                signAccount: '',
                signPassword: '',
                key: 'BB844B7CFD8C40E8D274A1F31684246973359'
            },
            rules: {
                account: [
                    { required: true, message: '请输入账号', trigger: 'blur' }
                ],
                password: [
                    { required: true, message: '请输入密码', trigger: 'blur' }
                ],
                code: [
                    { required: true, message: '请输入验证码', trigger: 'blur' }
                ]
            },
            codeImg: '',
            loading: false
        }
    },
    methods: {
        login () {
            this.$refs.form.validate(async (valid) => {
                if (valid) {
                    this.loading = true
                    const res = await this.$store.dispatch('user/getLogin', this.form)
                    this.loading = false
                    await this.getCode()
                    this.form.code = ''
                    if (res) await this.$router.push('/dashboard')
                } else {
                    return false
                }
            })
        },
        async getCode () {
            const res = await getCode(this.form.key)
            this.codeImg = window.URL.createObjectURL(res.data)
        }
    }
}
</script>

<style scoped lang="scss">
* {
    box-sizing: border-box;
}

.LoginPage {
    width: 100vw;
    height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    position: relative;
    background-color: rgba(41, 57, 87, 0.2);

    .box-card {
        display: flex;
        justify-content: center;
        width: 450px;
        height: 490px;
        border-radius: 10px;
        padding: 34px 0;

        .logo {
            display: flex;
            justify-content: center;

            img {
                height: 54px;
                margin-bottom: 34px;
            }
        }

        p {
            font-size: 14px;
            color: #FD3333;
        }
    }

    .copyright {
        color: white;
        font-size: 14px;
        position: fixed;
        bottom: 20px;

        span {
            margin: 0 10px;
        }
    }

    video {
        position: absolute;
        z-index: -1;
        width: 100%;
        height: 100%;
        object-fit: cover;
        background-color: #FFFFFF;
    }

    ::v-deep .el-card__body {
        padding: 0;

        .el-form, .el-form-item, .el-button--primary {
            width: 370px;
        }

        .el-form-item__content {
            position: relative;

            input {
                height: 50px;
                padding: 0 42px;
            }

            .codeInp input {
                padding: 0 116px 0 42px;
            }

            input::-webkit-outer-spin-button,
            input::-webkit-inner-spin-button {
                -webkit-appearance: none !important;
            }

            img:first-of-type {
                position: absolute;
                left: 15px;
                top: 17px;
                height: 15px;
            }

            .code {
                position: absolute;
                right: 15px;
                top: 8px;
                height: 32px;
            }

            button {
                height: 54px;
            }
        }
    }
}
</style>
